﻿using System;
using System.Collections.Generic;
using BackgroundQueueServiceSample.Model;
using BlueDot.Mobile.Core;

namespace BackgroundQueueServiceSample.Views
{
    public partial class DataView : ViewBase
    {
        #region Fields

        private const string DataViewShown = WellKnownActions.ViewShown + "/DataView";
        private const string DataViewShowning = WellKnownActions.ViewShowing + "/DataView";
        private bool dataLoadDone;

        #endregion

        #region Constructors

        public DataView()
        {
            InitializeComponent();
        }

        [DependencyConstructor]
        public DataView(IMobileApplication application)
            : base(application)
        {
            InitializeComponent();
        }

        #endregion

        #region public

        [Receiver(ApplicationActions.MainViewDataLoadDone, UpdateMode = UpdateMode.WhenActive)]
        public void DataLoadDone(object sender, DataEventArgs<List<Person>> args)
        {
            dataLoadDone = true;

            SuspendLayout();

            personBindingSource.DataSource = args.Data;

            Application.Actions[WellKnownActions.HideProgressPanel].Fire(this, EventArgs.Empty);

            ResumeLayout();
        }

        [Receiver(DataViewShown)]
        public void DataViewShownAction(object sender, EventArgs e)
        {
            ((Shell) Application.Shell).Menu = mainMenu;
        }

        [Receiver(DataViewShowning)]
        public void DataViewShowningAction(object sender, EventArgs e)
        {
            if (dataLoadDone == false)
            {
                Application.Actions[WellKnownActions.ShowProgressPanel].Fire(this, EventArgs.Empty);
            }
        }

        #endregion
    }
}